package com.junmoo.www.config.swagger;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.schema.Example;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

@EnableOpenApi
//@EnableKnife4j
@Configuration
public class Swagger3Config {

    @Bean
    public Docket docket() {

        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
//                .enable(true)
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build()
                // 统一设置tag
                .securitySchemes( securitySchemes())
                .securityContexts(securityContexts())
                .globalRequestParameters(setHeader())
                ;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            .title("Swagger3接口文档")
            .description("更多请咨询服务开发者Ray。")
            .contact(new Contact("junmoo", "http://www.baidu.cn", "junmoo@foxmail.com"))
            .version("1.0")
            .build();
    }


    /**
     * 设置授权信息
     */
    private List<SecurityScheme> securitySchemes() {
//        HttpAuthenticationScheme jwt = HttpAuthenticationScheme.JWT_BEARER_BUILDER.name("JWT").build();
        ApiKey apiKey = new ApiKey("BearerToken", "Authorization", "header");
        return Arrays.asList(apiKey);
    }

    /**
     * 授权信息全局应用
     */
    private List<SecurityContext> securityContexts() {
        SecurityContext s1 = SecurityContext.builder()
                .securityReferences( defaultAuth() )
                .operationSelector(o -> o.requestMappingPattern().matches("/.*"))
                .build();
        return Arrays.asList(s1);
    }
    /**
     * bearer 授权范围
     * @return List<SecurityReference>
     */
    private List<SecurityReference> defaultAuth() {
        SecurityReference s1= SecurityReference.builder().scopes(new AuthorizationScope[0]) .reference("JWT") .build();
        return Arrays.asList(s1);
    }

//------------------------------- 设置全局参数 ，这里设置的是全局请求头参数 ------------------------------------------------------------------------
    private List<RequestParameter> setHeader(){
        return Arrays.asList(
                buildHeader("deviceNo"),  buildHeader("deviceType"),
                buildHeader("userId"), buildHeader("version"), buildHeader("token")
        );
    }
    private RequestParameter buildHeader(String name){
//        String name="deviceNo";
        String description="";
        ParameterType type=ParameterType.HEADER;
        int precedence=1; //  优先权
        int parameterIndex=1;// 索引

        Boolean required=false;
        Boolean deprecated=false;
        Boolean hidden=false;
        ParameterSpecification parameterSpecification=new ParameterSpecification( null, null);
        Example scalarExample=new Example("id",  "summary", "description", "value",  "externalValue", "mediaType");
        Collection<Example> examples=new ArrayList<>();

        List<VendorExtension> extensions=new ArrayList<>();
        RequestParameter requestParameter = new RequestParameter(name,type,description,required,deprecated,hidden
                ,parameterSpecification,scalarExample,examples,precedence,extensions,parameterIndex);
        return  requestParameter;
    }


}
